/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.app.skyview;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.logging.Logger;
import space.app.common.Application;
import space.app.common.ApplicationBuilder;
import space.app.common.Server;
import space.app.common.ServerFrame;

/**
 *
 * @author Pierre
 */
public class Main implements ApplicationBuilder<TextureMessage> {

    static final Logger logger = Logger.getLogger(Main.class.getName());
    final URL boxDescriptionFile;

    public Main(URL boxDescriptionFile) {
        this.boxDescriptionFile = boxDescriptionFile;
    }

    /**
     * @param args
     * <ul>
     * <li>no args</li>
     * <li>-open "filepath"</li>
     * <ul>
     */
    public static void main(String args[]) throws MalformedURLException {
        
        /**
         * Parse the command line arguments
         */
        if (args == null) {
            throw new IllegalArgumentException("Unexpected code path");
        }
        final URL boxDescriptionFile;
        if (args.length == 0) {
            boxDescriptionFile = null;
        } else if (args.length == 2 && args[0].equals("-open")) {
            final File file = new File(args[1]);
            if(file.isFile())
                boxDescriptionFile = file.toURI().toURL();
            else
                boxDescriptionFile = new URL(args[1]);
        } else {
            throw new IllegalArgumentException("The program argument error:" + Arrays.asList(
                    args));
        }
        /**
         * Select the Nimbus look
         */
        Application.run(new Main(boxDescriptionFile));

    }

    public ServerFrame<TextureMessage> createFrame(Application<TextureMessage> application) {
        return new SkyViewFrame(application, boxDescriptionFile);
    }

    public Server<TextureMessage> createServer(Application<TextureMessage> application) {
        return new SkyServer(application);
    }
}
